//
//  OperationBillDetailAddressCell.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/11/9.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "OperationBillDetailAddressCell.h"

@interface OperationBillDetailAddressCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *addressBgView;
@property (nonatomic, strong) UILabel *fromAddLab;
@property (nonatomic, strong) UIImageView *statusImgView;
@property (nonatomic, strong) UILabel *toAddLab;

@property (nonatomic, strong) UILabel *jieTitleLab;
@property (nonatomic, strong) UIView *jieView;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UILabel *jiaoTitleLab;
@property (nonatomic, strong) UIView *jiaoView;

@end

@implementation OperationBillDetailAddressCell

+ (instancetype)OperationBillDetailAddressCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"OperationBillDetailAddressCell";
    OperationBillDetailAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[OperationBillDetailAddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [UIView new];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.addressBgView = [UIView new];
        self.addressBgView.backgroundColor = HexColor(0xF7F7FA);
        self.addressBgView.layer.masksToBounds = YES;
        self.addressBgView.layer.cornerRadius = 2;
        [self.bgView addSubview:self.addressBgView];
        
        self.fromAddLab = [UILabel new];
        self.fromAddLab.textColor = HexColor(0x2B2C44);
        self.fromAddLab.font = BoldFontOfSize(20);
        self.fromAddLab.textAlignment = NSTextAlignmentCenter;
        [self.addressBgView addSubview:self.fromAddLab];
        
        self.statusImgView = [UIImageView new];
        [self.addressBgView addSubview:self.statusImgView];
        
        self.toAddLab = [UILabel new];
        self.toAddLab.textColor = HexColor(0x2B2C44);
        self.toAddLab.font = BoldFontOfSize(20);
        self.toAddLab.textAlignment = NSTextAlignmentCenter;
        [self.addressBgView addSubview:self.toAddLab];
        
        self.jieTitleLab = [UILabel new];
        self.jieTitleLab.textColor = HexColor(0x2B2C44);
        self.jieTitleLab.font = BoldFontOfSize(18);
        [self.bgView addSubview:self.jieTitleLab];
        
        self.jieView = [UIView new];
        [self.bgView addSubview:self.jieView];
        
        self.line = [UILabel new];
        self.line.backgroundColor = Color_backgroundColor;
        [self.bgView addSubview:self.line];
        
        self.jiaoTitleLab = [UILabel new];
        self.jiaoTitleLab.textColor = HexColor(0x2B2C44);
        self.jiaoTitleLab.font = BoldFontOfSize(18);
        [self.bgView addSubview:self.jiaoTitleLab];
        
        self.jiaoView = [UIView new];
        [self.bgView addSubview:self.jiaoView];
    }
    return self;
}

-(void)setModel:(OperationBillDetailModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bgView.nim_left = 15;
    self.bgView.nim_top = 10;
    self.bgView.nim_width = ScreenW - 30;
    
    self.addressBgView.frame = CGRectMake(10, 10, self.bgView.nim_width - 2 * 10, 69);
    
    self.statusImgView.nim_width = 59;
    self.statusImgView.nim_height = 10;
    self.statusImgView.nim_left = (self.addressBgView.nim_width - self.statusImgView.nim_width) * 0.5;
    self.statusImgView.nim_top = 30;
    [self.statusImgView setImage:[UIImage imageNamed:@"operationbill_status"]];
    
    self.fromAddLab.nim_left = 10;
    self.fromAddLab.text = self.model.start_city;
    [self.fromAddLab sizeToFit];
    self.fromAddLab.nim_width = self.statusImgView.nim_left - 20;
    self.fromAddLab.nim_centerY = self.statusImgView.nim_centerY;
    
    self.toAddLab.nim_left = 10 + self.statusImgView.nim_right;
    self.toAddLab.text = self.model.end_city;
    [self.toAddLab sizeToFit];
    self.toAddLab.nim_width = self.statusImgView.nim_left - 20;
    self.toAddLab.nim_centerY = self.statusImgView.nim_centerY;
    
    self.jieTitleLab.text = @"接货地址";
    self.jieTitleLab.nim_left = 15;
    self.jieTitleLab.nim_top = self.addressBgView.nim_bottom + 15;
    [self.jieTitleLab sizeToFit];
    
    self.jieView.nim_left = 15;
    self.jieView.nim_top = self.jieTitleLab.nim_bottom + 5;
    self.jieView.nim_width = self.bgView.nim_width - 30;
    
    [self.jieView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    __block CGFloat pickTop = 0;
    [self.model.pickup_address enumerateObjectsUsingBlock:^(OperationBillAddressModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.arrive_time = @"";
        OperationBillDetailAddressView *view = [[OperationBillDetailAddressView alloc] initWithFrame:CGRectMake(0, pickTop, self.jieView.nim_width, 0)];
        view.model = obj;
        view.nim_height = view.height;
        if (idx == self.model.pickup_address.count - 1) {
            view.dismissLine = YES;
        }
        view.iconUrl = @"icon_hair";
        [self.jieView addSubview:view];
        pickTop = view.nim_bottom;
    }];
    
    self.jieView.nim_height = pickTop;
    
    self.line.frame = CGRectMake(10, self.jieView.nim_bottom, self.bgView.nim_width - 20, 1);
    
    self.jiaoTitleLab.text = @"交货地址";
    self.jiaoTitleLab.nim_left = 15;
    self.jiaoTitleLab.nim_top = self.line.nim_bottom + 15;
    [self.jiaoTitleLab sizeToFit];
    
    self.jiaoView.nim_left = 15;
    self.jiaoView.nim_top = self.jiaoTitleLab.nim_bottom + 5;
    self.jiaoView.nim_width = self.bgView.nim_width - 30;
    
    [self.jiaoView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    __block CGFloat delTop = 0;
    [self.model.delivery_address enumerateObjectsUsingBlock:^(OperationBillAddressModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        OperationBillDetailAddressView *view = [[OperationBillDetailAddressView alloc] initWithFrame:CGRectMake(0, delTop, self.jiaoView.nim_width, 0)];
        view.model = obj;
        view.nim_height = view.height;
        if (idx == self.model.delivery_address.count - 1) {
            view.dismissLine = YES;
        }
        view.iconUrl = @"icon_add_shou";
        [self.jiaoView addSubview:view];
        delTop = view.nim_bottom;
    }];
    
    self.jiaoView.nim_height = delTop;
    
    self.bgView.nim_height = self.jiaoView.nim_bottom;
    self.height = self.bgView.nim_bottom;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface OperationBillDetailAddressView () {
    CGFloat viewW;
}

@property (nonatomic, strong) UIImageView *iconImgView;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *addressLab;
@property (nonatomic, strong) UILabel *contactLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *line;

@end

@implementation OperationBillDetailAddressView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HexColor(0xffffff);
        viewW = frame.size.width;
        
        self.iconImgView = [UIImageView new];
        [self addSubview:self.iconImgView];
        
        self.nameLab = [UILabel new];
        self.nameLab.textColor = HexColor(0x2B2C44);
        self.nameLab.font = BoldFontOfSize(16);
        [self addSubview:self.nameLab];
        
        self.addressLab = [UILabel new];
        self.addressLab.textColor = HexColor(0x9C9DB3);
        self.addressLab.font = [UIFont systemFontOfSize:14];
        self.addressLab.numberOfLines = 0;
        [self addSubview:self.addressLab];
        
        self.contactLab = [UILabel new];
        self.contactLab.textColor = HexColor(0x9C9DB3);
        self.contactLab.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.contactLab];
        
        self.timeLab = [UILabel new];
        self.timeLab.textColor = HexColor(0x9C9DB3);
        self.timeLab.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.timeLab];
        
        self.line = [UILabel new];
        self.line.backgroundColor = Color_backgroundColor;
        [self addSubview:self.line];
    }
    return self;
}

-(void)setModel:(OperationBillAddressModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.iconImgView.nim_left = 0;
    self.iconImgView.nim_top = 10;
    self.iconImgView.nim_width = 22;
    self.iconImgView.nim_height = 22;
    
    self.nameLab.text = [FactoryMethod isNull:self.model.name] ? @"名字" : self.model.name;
    self.nameLab.nim_left = self.iconImgView.nim_right + 15;
    [self.nameLab sizeToFit];
    self.nameLab.nim_width = viewW - self.nameLab.nim_left;
    self.nameLab.nim_centerY = self.iconImgView.nim_centerY;
    
    self.addressLab.text = [FactoryMethod isNull:self.model.info] ? @"地址" : self.model.info;
    self.addressLab.nim_left = self.iconImgView.nim_right + 15;
    self.addressLab.nim_width = viewW - self.addressLab.nim_left;
    [self.addressLab sizeToFit];
    self.addressLab.nim_width = viewW - self.addressLab.nim_left;
    self.addressLab.nim_top = self.nameLab.nim_bottom + 10;
    
    self.contactLab.text = [FactoryMethod isNull:self.model.contract_name] ? @"联系人" : [NSString stringWithFormat:@"%@，%@",self.model.contract_name,self.model.contract_tel];
    self.contactLab.nim_left = self.iconImgView.nim_right + 15;
    [self.contactLab sizeToFit];
    self.contactLab.nim_width = viewW - self.contactLab.nim_left;
    self.contactLab.nim_top = self.addressLab.nim_bottom + 3;
    
    if ([FactoryMethod isNull:self.model.arrive_time]) {
        self.timeLab.frame = CGRectZero;
        self.height = self.contactLab.nim_bottom + 10;
    }else {
        self.timeLab.text = [NSString stringWithFormat:@"要求到达时间：%@",self.model.arrive_time];
        self.timeLab.nim_left = self.contactLab.nim_left;
        self.timeLab.nim_top = self.contactLab.nim_bottom + 3;
        [self.timeLab sizeToFit];
        
        self.height = self.timeLab.nim_bottom + 10;
    }
    
    self.line.frame = CGRectMake(self.contactLab.nim_left, self.height - 1, viewW - self.contactLab.nim_left, 1);
}

-(void)setIconUrl:(NSString *)iconUrl {
    _iconUrl = iconUrl;
    [self.iconImgView setImage:[UIImage imageNamed:iconUrl]];
}

-(void)setDismissLine:(BOOL)dismissLine {
    _dismissLine = dismissLine;
    self.line.hidden = dismissLine;
}

@end
